//
//  InformationViewController.m
//  B_WatchHome
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 DHF. All rights reserved.
//

#import "InformationViewController.h"
#import "InforDetailViewController.h"
#import "InforRotateModel.h"
#import "InforTableModel.h"
#import "InforTableViewCell.h"
#import "SDCycleScrollView.h"
#import "AppTools.h"
#import <MJRefresh.h>
#import <UIImageView+WebCache.h>
#import <Masonry.h>

@interface InformationViewController ()<UITableViewDataSource, UITableViewDelegate, rotateViewDelegate>

@property (nonatomic, strong)UITableView *infoTableView;

@property (nonatomic, assign)NSInteger i;
@end

@implementation InformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.infoTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    
    self.infoTableView.backgroundColor = [UIColor whiteColor];
    self.infoTableView.delegate = self;
    self.infoTableView.dataSource = self;
    
    [self.infoTableView registerClass:[InforTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.infoTableView];
    
    //头部区域
    self.infoTableView.tableHeaderView = [[UIView alloc] init];
    [self.infoTableView.tableHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.and.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-400);
    }];
    
    self.infoTableView.tableHeaderView.backgroundColor = [UIColor whiteColor];
    
    //初始化轮播图 cell数组
    self.tableViewModelArray = [NSMutableArray array];
    self.modelArray = [NSMutableArray array];
    
    self.i = 0;
    
    
    [self getAFNetWorkingData];
    
    [self addFooterRefresh];
    
    [self addHeaderRefresh];
    
}

//下拉
- (void)addHeaderRefresh
{
    self.infoTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getAFNetWorkingData];
        
    }];
    [self.infoTableView.mj_header beginRefreshing];
    
}

//上拉
- (void)addFooterRefresh
{
    self.infoTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        self.i++;
        [self getAFNetWorkingData];
    }];
    [self.infoTableView.mj_footer beginRefreshing];
}

//AFN网络请求
- (void)getAFNetWorkingData{
    NSString *tableviewURL = [NSString stringWithFormat:@"http://www.xbiao.com/app/articlelist?&pages=%ld&type=1&time=1439802437825",self.i];
    if (self.i == 0) {
        [AppTools getDataWithURL:tableviewURL Block:^(id result) {
            
            [self.tableViewModelArray removeAllObjects];
            
            for (NSMutableDictionary *dic in result) {
                InforTableModel *model = [[InforTableModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.tableViewModelArray addObject:model];
            }
            
            [self.infoTableView reloadData];
            [self.infoTableView.mj_header endRefreshing];
        }];
        
        //轮播图
        self.rotateView = [[RotateView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,  200)];
        self.rotateView.myArray = [NSMutableArray array];
        self.rotateView.rotateDelegate = self;
        self.infoTableView.tableHeaderView = self.rotateView;
    
        NSString *urlStr = @"http://www.xbiao.com/app/articleimage";
        
        [AppTools getDataWithURL:urlStr Block:^(id result) {
            
            [self.modelArray removeAllObjects];
            
            NSMutableArray  *imageArray = result;
            
            for (NSMutableDictionary *dic in imageArray) {
                
                InforRotateModel *infomodel = [[InforRotateModel alloc]init];
                
                [infomodel setValuesForKeysWithDictionary:dic];
                
                [self.rotateView.myArray addObject:infomodel];
                
            }
            
            [_rotateView createSubviews];
            
        }];
        
        
    }
    else
    {
        [AppTools getDataWithURL:tableviewURL Block:^(id result) {
            
            for (NSMutableDictionary *dic in result) {
                InforTableModel *model = [[InforTableModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.tableViewModelArray addObject:model];
            }
            [self.infoTableView reloadData];
            [self.infoTableView.mj_footer endRefreshing];
        }];
    }
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableViewModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    
    InforTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    InforTableModel *model = [self.tableViewModelArray objectAtIndex:indexPath.row];
    cell.model = model;
    
    return  cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    InforDetailViewController *inforDeta = [[InforDetailViewController alloc] init];
    
    InforTableModel *model = [self.tableViewModelArray objectAtIndex:indexPath.row];
    
    inforDeta.model = model;
    
    [self.navigationController pushViewController:inforDeta animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
